import createHandler from './index'
import { v4 } from "uuid"
import { selectOne, insertOne, selectAll, selectByProperty, remove } from "./fake-db"
import { getLoggedUser } from "./auth"

/**
 *  code: 6000-7000
 */

const addItemToCart = createHandler((itemId, optionKey, number) => {
    const curUser = getLoggedUser()
    if (!curUser) return {
        code: 101,
        msg: '登录过期'
    }
    if (number < 1) return {
        code: 6001,
        msg: '至少购买1件'
    }
    const item = selectOne('item', 'id', itemId)
    if (!item) return {
        code: 6002,
        msg: '商品不存在'
    }
    const option = item.options.find(option => option.key === optionKey)
    if (!option) return {
        code: 6003,
        msg: '配置不存在'
    }
    const cart = selectAll('cart')
    const oldRecordIdx = cart.findIndex(
        row => row.uid === curUser.id && row.itemId === itemId && row.optionKey === optionKey
    )
    if (oldRecordIdx === -1) {
        if (option.number < number) return {
            code: 6004,
            msg: '库存不足'
        }
        insertOne('cart', {
            id: v4(),
            uid: curUser.id,
            itemId,
            optionKey,
            number
        })
    } else {
        if (option.number < cart[oldRecordIdx].number + number) return {
            code: 6004,
            msg: '库存不足'
        }
        const record = cart.splice(oldRecordIdx, 1)[0]
        record.number += number
        cart.unshift(record)
    }
    return {
        code: 0
    }
})
export { addItemToCart }

/**
 * 获取购物车的商品列表（
 */
const getCartItemList = createHandler(() => {
    const curUser = getLoggedUser()
    if (!curUser) return {
        code: 101,
        msg: '登录过期'
    }
    const cart = selectAll('cart')

    const tempList = cart.filter(
        row => row.uid === curUser.id
    )

    const cartList = tempList.map((temp) => {
        const item = selectOne('item', 'id', temp.itemId)
        const option = item.options.filter(row => row.key === temp.optionKey)
        return {
            id: temp.id,
            uid: temp.uid,
            item: item,
            number: temp.number,
            selected: false,
            option: option,
        }
    })

    return {
        code: 0,
        data: cartList
    }
})
export { getCartItemList }

//修改商品数量
const updateCartItem = createHandler((id, number) => {
    const curUser = getLoggedUser()
    if (!curUser) return {
        code: 101,
        msg: '登录过期'
    }
    const cart = selectOne('cart', 'id', id)
    if (!cart || cart.uid !== curUser.id) return {
        code: 6005,
        msg: '购物车记录不存在'
    }
    if (number < 1) return {
        code: 6001,
        msg: '至少购买1件'
    }
    const item = selectOne('item', 'id', cart.itemId)
    if (!item) return {
        code: 6002,
        msg: '商品不存在'
    }
    const option = item.options.find(option => option.key === cart.optionKey)
    if (!option) return {
        code: 6003,
        msg: '配置不存在'
    }
    if (option.number < number) return {
        code: 6004,
        msg: '库存不足'
    }
    return {
        code: 0
    }

})
export { updateCartItem }

//删除商品
const deleteCartItem = createHandler((id) => {
    const result = selectByProperty('cart', 'id', id)
    if (result.length < 0) return {
        code: 1,
        msg: '购物车记录不存在'
    }
    remove('cart', 'id', id)
    return {
        code: 0
    }
})
export { deleteCartItem }